package com.example.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

public class StuListActivity extends AppCompatActivity implements IOnDelListener, IOnClickItemListener{
    private RecyclerView rv_stu;
    private StudentAdapter adapter;
    private ArrayList<Student> studentArrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu_list);

        setTitle("学生列表");

        rv_stu = findViewById(R.id.rv_stu);
        adapter = new StudentAdapter(studentArrayList, this, this);
        rv_stu.setAdapter(adapter);
        rv_stu.setLayoutManager(new LinearLayoutManager(StuListActivity.this));

        getDataFromBmob();
    }

    private void getDataFromBmob() {
        BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
        studentBmobQuery.findObjects(new FindListener<Student>() {
            @Override
            public void done(List<Student> list, BmobException e) {
                if(e == null){
                    studentArrayList.addAll(list);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if(id == R.id.action_add){
            Intent intent = new Intent(StuListActivity.this, AddActivity.class);
            startActivityForResult(intent, 1001);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == 2002){
            //添加到数组中
            Student student = (Student) data.getSerializableExtra("STU");
            Log.e("aa", "onActivityResult: "+ student );
            studentArrayList.add(student);
            adapter.notifyDataSetChanged();

            //保存到Bmob的Student表中
            student.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {

                }
            });
        }

        if(resultCode == 3002){
            Student student = (Student) data.getSerializableExtra("STUDENT");
            for(Student s: studentArrayList) {
                if(s.getObjectId().equals(student.getObjectId())) {
                    s.setName(student.getName());
                    s.setAge(student.getAge());
                    s.setProfession(student.getProfession());
                    s.setScore(student.getScore());
                    s.setHeadimg(student.getHeadimg());
                    Log.e("bb", "path = " + student.getHeadimg().getUrl() );

                    adapter.notifyDataSetChanged();
                }
            }
        }
    }

    //通过objectId删除学生
    @Override
    public void del(String objectId) {
        //查询时列表数据的删除
        for(Student student: studentArrayList){
            if(student.getObjectId().equals(objectId)){
                studentArrayList.remove(student);
                adapter.notifyDataSetChanged();
            }
        }

        //Bmob中数据的删除
        BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
        studentBmobQuery.addWhereEqualTo("objectId", objectId);
        studentBmobQuery.findObjects(new FindListener<Student>() {
            @Override
            public void done(List<Student> list, BmobException e) {
                if(e == null){
                    for(final Student student: list){
                        student.delete(new UpdateListener() {
                            @Override
                            public void done(BmobException e) {

                            }
                        });
                    }
                }
            }
        });
    }

    //编辑整个item，即学生信息
    @Override
    public void click(View view, final int i) {
        //双击
//        view.setOnClickListener(new View.OnClickListener() {
////            @Override
////            public void onClick(View v) {
////            }
////        });

        //单击
        Student s = studentArrayList.get(i);

        Intent intent = new Intent();
        //将数据传递到下一个活动
        intent.putExtra("STU",s);

        intent.setClass(StuListActivity.this,EditActivity.class);
        startActivityForResult(intent,1002);
    }
}
